package myjava.awt.datatransfer;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Reader;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import org.apache.harmony.awt.datatransfer.DTK;
import org.apache.harmony.awt.internal.nls.Messages;

public class DataFlavor implements Externalizable, Cloneable {
    public static final DataFlavor javaFileListFlavor = new DataFlavor("application/x-java-file-list; class=java.util.List", "application/x-java-file-list");
    @Deprecated
    public static final DataFlavor plainTextFlavor = new DataFlavor("text/plain; charset=unicode; class=java.io.InputStream", "Plain Text");
    private static DataFlavor plainUnicodeFlavor = null;
    private static final String[] sortedTextFlavors = new String[]{"text/sgml", "text/xml", "text/html", "text/rtf", "text/enriched", "text/richtext", "text/uri-list", "text/tab-separated-values", "text/t140", "text/rfc822-headers", "text/parityfec", "text/directory", "text/css", "text/calendar", "application/x-java-serialized-object", "text/plain"};
    public static final DataFlavor stringFlavor = new DataFlavor("application/x-java-serialized-object; class=java.lang.String", "Unicode String");
    private String humanPresentableName;
    private MimeType mimeInfo;
    private Class<?> representationClass;

    private static boolean isCharsetSupported(String charset) {
        try {
            return Charset.isSupported(charset);
        } catch (IllegalCharsetNameException e) {
            return false;
        }
    }

    public DataFlavor() {
        this.mimeInfo = null;
        this.humanPresentableName = null;
        this.representationClass = null;
    }

    public DataFlavor(String mimeType, String humanPresentableName) {
        try {
            init(mimeType, humanPresentableName, null);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(Messages.getString("awt.16C", this.mimeInfo.getParameter("class")), e);
        }
    }

    private void init(String mimeType, String humanPresentableName, ClassLoader classLoader) throws ClassNotFoundException {
        try {
            Class cls;
            this.mimeInfo = MimeTypeProcessor.parse(mimeType);
            if (humanPresentableName != null) {
                this.humanPresentableName = humanPresentableName;
            } else {
                this.humanPresentableName = new StringBuilder(String.valueOf(this.mimeInfo.getPrimaryType())).append('/').append(this.mimeInfo.getSubType()).toString();
            }
            String className = this.mimeInfo.getParameter("class");
            if (className == null) {
                className = "java.io.InputStream";
                this.mimeInfo.addParameter("class", className);
            }
            if (classLoader == null) {
                cls = Class.forName(className);
            } else {
                cls = classLoader.loadClass(className);
            }
            this.representationClass = cls;
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(Messages.getString("awt.16D", (Object) mimeType));
        }
    }

    private String getCharset() {
        if (this.mimeInfo == null || isCharsetRedundant()) {
            return "";
        }
        String charset = this.mimeInfo.getParameter("charset");
        if (isCharsetRequired() && (charset == null || charset.length() == 0)) {
            return DTK.getDTK().getDefaultCharset();
        }
        if (charset == null) {
            return "";
        }
        return charset;
    }

    private boolean isCharsetRequired() {
        String type = this.mimeInfo.getFullType();
        return type.equals("text/sgml") || type.equals("text/xml") || type.equals("text/html") || type.equals("text/enriched") || type.equals("text/richtext") || type.equals("text/uri-list") || type.equals("text/directory") || type.equals("text/css") || type.equals("text/calendar") || type.equals("application/x-java-serialized-object") || type.equals("text/plain");
    }

    private boolean isCharsetRedundant() {
        String type = this.mimeInfo.getFullType();
        return type.equals("text/rtf") || type.equals("text/tab-separated-values") || type.equals("text/t140") || type.equals("text/rfc822-headers") || type.equals("text/parityfec");
    }

    public String getMimeType() {
        return this.mimeInfo != null ? MimeTypeProcessor.assemble(this.mimeInfo) : null;
    }

    public Class<?> getRepresentationClass() {
        return this.representationClass;
    }

    public final boolean isMimeTypeEqual(DataFlavor dataFlavor) {
        if (this.mimeInfo != null) {
            return this.mimeInfo.equals(dataFlavor.mimeInfo);
        }
        return dataFlavor.mimeInfo == null;
    }

    public synchronized void writeExternal(ObjectOutput os) throws IOException {
        os.writeObject(this.humanPresentableName);
        os.writeObject(this.mimeInfo);
    }

    public synchronized void readExternal(ObjectInput is) throws IOException, ClassNotFoundException {
        this.humanPresentableName = (String) is.readObject();
        this.mimeInfo = (MimeType) is.readObject();
        this.representationClass = this.mimeInfo != null ? Class.forName(this.mimeInfo.getParameter("class")) : null;
    }

    public Object clone() throws CloneNotSupportedException {
        MimeType mimeType;
        DataFlavor clone = new DataFlavor();
        clone.humanPresentableName = this.humanPresentableName;
        clone.representationClass = this.representationClass;
        if (this.mimeInfo != null) {
            mimeType = (MimeType) this.mimeInfo.clone();
        } else {
            mimeType = null;
        }
        clone.mimeInfo = mimeType;
        return clone;
    }

    public String toString() {
        return new StringBuilder(String.valueOf(getClass().getName())).append("[MimeType=(").append(getMimeType()).append(");humanPresentableName=").append(this.humanPresentableName).append("]").toString();
    }

    public boolean equals(Object o) {
        if (o == null || !(o instanceof DataFlavor)) {
            return false;
        }
        return equals((DataFlavor) o);
    }

    public boolean equals(DataFlavor that) {
        if (that == this) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (this.mimeInfo == null) {
            if (that.mimeInfo != null) {
                return false;
            }
            return true;
        } else if (!this.mimeInfo.equals(that.mimeInfo) || !this.representationClass.equals(that.representationClass)) {
            return false;
        } else {
            if (!this.mimeInfo.getPrimaryType().equals("text") || isUnicodeFlavor()) {
                return true;
            }
            String charset1 = getCharset();
            String charset2 = that.getCharset();
            if (isCharsetSupported(charset1) && isCharsetSupported(charset2)) {
                return Charset.forName(charset1).equals(Charset.forName(charset2));
            }
            return charset1.equalsIgnoreCase(charset2);
        }
    }

    public int hashCode() {
        return getKeyInfo().hashCode();
    }

    private String getKeyInfo() {
        String key = new StringBuilder(String.valueOf(this.mimeInfo.getFullType())).append(";class=").append(this.representationClass.getName()).toString();
        return (!this.mimeInfo.getPrimaryType().equals("text") || isUnicodeFlavor()) ? key : new StringBuilder(String.valueOf(key)).append(";charset=").append(getCharset().toLowerCase()).toString();
    }

    private boolean isUnicodeFlavor() {
        return this.representationClass != null && (this.representationClass.equals(Reader.class) || this.representationClass.equals(String.class) || this.representationClass.equals(CharBuffer.class) || this.representationClass.equals(char[].class));
    }
}
